<#-- 包含其他文件的内容 -->
<#include "../../common/header.ftl"/>
<script>
    $(function () {
        // 查询指定商品详情
        $(".showBtn").click(function () {
            // 发送ajax请求获取商品详情的内容
            $.get("/product/getContentById.do", {"id": $(this).data("id")}, function (data) {
                $("#showContentModal .modal-body").html(data.content);
            });
            $("#showContentModal").modal("show");
        });

        $(".inputBtn").click(function () {
            // 清空模态框内容
            $("#editForm")[0].reset();
            $("#dirSelect").html("");
            // 清除图片
            $("#coverUrl").attr("src", "");
            // 获取json数据
            var json = $(this).data("json");
            // console.log(json);
            // 将数据设置到模态框中
            if (json) {
                // 封面回显
                $("#coverUrl").attr("src", json.imgurl);
                $("#editForm input[name='id']").val(json.id);
                $("#editForm input[name='content.id']").val(json.conId);
                $("#editForm input[name='name']").val(json.name);
                $("#editForm input[name='price']").val(json.price);
                $("#editForm input[name='subTitle']").val(json.subTitle);
                $("#editForm input[name='imgurl']").val(json.imgurl);
                $("#editForm input[name='sequence']").val(json.sequence);
                $("#editForm input[name='storeNum']").val(json.storeNum);
                $("#editForm select[name='status']").val(json.status);
                $(".changeBtn").html(" 修改主图");
                // 商品父分类下拉框回显
                $("#parentSelect").val(json.parentId);
                // 商品分类下拉框回显
                // 查询商品父类型
                $.get("/productDir/listByParentId.do", {"parentId": json.parentId}, function (data) {
                    var temp = '';
                    // 将数据设置到商品分类
                    $.each(data, function (index, ele) {
                        temp += "<option value=\"" + ele.id + "\">" + ele.dirName + "</option>";
                    });
                    $("#dirSelect").html(temp);
                    // 回显操作,一定要在遍历拼接之后
                    $("#parentSelect").val(json.parentId);
                });
                // 查询文章的内容
                $.get("/product/getContentById.do", {"id": json.conId}, function (data) {
                    // 设置到富文本中
                    ck.setData(data.content);
                });
            } else {
                // 设置按钮文字
                $(".changeBtn").html(" 添加主图");
                // 清空富文本内容
                ck.setData("");
            }
            // 显示模态框
            $("#inputModal").modal("show");
        });
        // 提交表单
        $(".submitBtn").click(function () {
            //获取富文本编辑器的内容,设置到表单组件中
            var data = ck.getData();
            $("#editor").html(data);
            $("#editForm").ajaxSubmit(function (data) {
                doSuccess(data);
            });
        });
        // 更换封面
        updateImgV2(".changeBtn", "imgurl");
    });


    // 二级联动
    $(function () {
        $("#parentSelect").change(function () {
            // 查询商品父类型
            $.get("/productDir/listByParentId.do", {"parentId": $(this).val()}, function (data) {
                var temp = '';
                // 将数据设置到商品分类
                $.each(data, function (index, ele) {
                    temp += "<option value=\"" + ele.id + "\">" + ele.dirName + "</option>";
                });
                $("#dirSelect").html(temp);
            });

        });
    });

    $(function () {
        // 下拉列表值改变后提交表单
        $("#statusSelect").change(function () {
            $("#searchForm").submit();
        });
    });

    $(function () {
        // 删除商品
        deleteData(".deleteBtn");
    });
</script>
</head>
<body>

<!--图片上传表单-->
<#include "../../common/img.ftl"/>

<div class="container " style="margin-top: 20px">
    <#include "../../common/top.ftl"/>
    <div class="row">
        <div class="col-sm-3">
        <#-- 往数据模型中填充一个属性 -->
            <#assign menu="product"/>
            <#include "../../common/menu.ftl"/>
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">商品管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/product/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label>关键字:</label>
                    <input type="text" class="form-control" name="keyword"
                           value="${(qo.keyword)!}" placeholder="商品名/副标题/商品类型">
                </div>
                <button id="btn_query" class="btn btn-default">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>
                <div class="form-group">
                    <label>发布状态:</label>
                    <select class="form-control" name="status" id="statusSelect">
                        <option value="-1">全部</option>
                        <option value="1">已发布</option>
                        <option value="2">未发布</option>
                    </select>
                    <script>
                        $("#statusSelect").val(${(qo.status)!});
                    </script>
                </div>
                <a role="button" class="btn btn-success inputBtn">
                    <span class="glyphicon glyphicon-plus"></span> 添加
                </a>
            </form>

            <table class="table table-striped table-hover">
                <tr>
                    <th>编号</th>
                    <th>商品名</th>
                    <th>副标题</th>
                    <th>主图</th>
                    <th>积分</th>
                    <th>类型</th>
                    <th>状态</th>
                    <th>权重</th>
                    <th>库存</th>
                    <th>销量</th>
                    <th>添加时间</th>
                    <th>操作</th>
                </tr>

            <#-- 循环迭代指令 -->
                <#list pageInfo.list as entity>
                    <tr>
                        <td>${entity_index + 1}</td>
                        <td>${entity.name}</td>
                        <td>${entity.subTitle}</td>
                        <td><img src="${entity.imgurl}" width="100px"/></td>
                        <td>${entity.price}</td>
                        <td>${(entity.dir.dirName)!}</td>
                        <td>${entity.stateName}</td>
                        <td>${(entity.sequence)!}</td>
                        <td>${(entity.storeNum)!}</td>
                        <td>${(entity.salesNum)!}</td>
                        <td>${(entity.addTime?string("yyyy-MM-dd HH:mm:ss"))!"暂未发布"}</td>
                        <td>
                            <a class="btn btn-warning btn-xs showBtn" data-id="${entity.content.id}">
                                <span class="glyphicon glyphicon-list-alt"></span> 查看详情
                            </a>
                            <a class="btn btn-info btn-xs inputBtn" data-json='${entity.json!}'>
                                <span class="glyphicon glyphicon-pencil"></span> 修改商品
                            </a>
                            <a class="btn btn-danger btn-xs deleteBtn" data-url="/product/delete.do?id=${entity.id}">
                                <span class="glyphicon glyphicon-trash"></span> 删除商品
                            </a>
                        </td>
                    </tr>
                </#list>
            </table>
            <#include "../../common/page.ftl"/>
        </div>
    </div>
</div>

<!-- 模态框必须作为body的子标签 -->
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">新增商品/编辑商品</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/product/saveOrUpdate.do" method="post" id="editForm">
                    <input type="text" name="id" hidden>
                    <input type="text" name="content.id" hidden>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">商品名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="name">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">商品副标题：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="subTitle">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">商品价格(积分)：</label>
                        <div class="col-sm-6">
                            <input type="number" class="form-control" name="price">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">商品主图：</label>
                        <div class="col-sm-6">
                            <img id="coverUrl" width="100%" class="changeBtn"/>
                            <a style="margin-left: 75px" class="btn btn-info glyphicon glyphicon-picture changeBtn">
                                更换主图</a>
                            <input type="hidden" class="form-control" name="imgurl">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">商品父分类：</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="parentSelect">
                                <option value="-1" selected>--- 请选择 ---</option>
                                    <#list parents as p>
                                        <option value="${p.id}">${p.dirName}</option>
                                    </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">商品分类：</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="dirSelect" name="dir.id">

                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">商品库存量：</label>
                        <div class="col-sm-6">
                            <input type="number" class="form-control" name="storeNum">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">排序权重：</label>
                        <div class="col-sm-6">
                            <input type="number" class="form-control" name="sequence">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">是否发布：</label>
                        <div class="col-sm-6">
                            <select class="form-control" name="status">
                                <option value="1">发布</option>
                                <option value="2">不发布</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <textarea name="content.content" id="editor" rows="10000" cols="80">
                        </textarea>
                        <script>
                            var ck = CKEDITOR.replace('editor');
                        </script>
                    </div>
                </form>


            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">
                    <span class="glyphicon glyphicon-off"></span> 关闭
                </button>
                <button type="button" class="btn btn-primary submitBtn">
                    <span class="glyphicon glyphicon-saved"></span> 保存
                </button>
            </div>
        </div>
    </div>
</div>
<!-- 模态框必须作为body的子标签 -->
<div class="modal fade" id="showContentModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">商品详情</h4>
            </div>
            <div class="modal-body">
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>
<style>
    #showContentModal img {
        width: 100%;
    }
</style>

</body>
</html>